<?php
class hotelAction extends globalAction{

    public function _initialize() {
		parent::_initialize();
        $this->assign('moduleTitle', '客栈');
	}


	public function test(){
		echo GROUP_NAME;
	}

	/**
	 * 获取推荐客栈栏目 暂以排序值排
	 */
	public function  gettuijianhotel(){
		$imr=M("hotel");
		$sqlrs="select hotel0,hotel1,hotel15 from ".DQ."hotel where hotel23=1212 order by hotel25 desc,hotel0 desc limit 0,20";
		$rstujname=$imr->query($sqlrs);
		$this->assign("rs",$rstujname);
		$temp= $this->fetch("hotel:gettuijianhotel");
		return $temp;
	}

	/**
	 * 获取本月最热门的客栈 暂以人气值排
	 */
	public function getrenqipaihang(){

		$imr=M("hotel");
		$sqlrs="select hotel0,hotel1 from ".DQ."hotel where hotel23=1212 order by hotel26 desc,hotel0 desc limit 0,20";
		$rshotname=$imr->query($sqlrs);
		$this->assign("rs",$rshotname);
		$temp= $this->fetch("hotel:getrenqipaihang");
		return $temp;
	}

	/**
	 * 查看酒店的具体的信息
	 *
	 */
	function view(){

		$id=$_REQUEST["id"];//酒店id
		
		if(!is_numeric($id)) die("请从正确的地址进入");

		$hmr=M("hotel");
		$cmr=A("Inn/classsys");
		$picmr=A("Inn/pic");

		$sql="select * from ".DQ."hotel where hotel0=".$id;
		$rs=$hmr->cache(true)->query($sql);
		//调用基础设施中文值
		if($rs[0]["hotel20"]!=""){
			$this->assign("jichushes",$cmr->getidtoname($rs[0]["hotel20"],1));
		}

		//调用客栈等级图片地址
		if($rs[0]["hotel6"]!=""){
			$this->assign("dengjipic",$cmr->getidtoname($rs[0]["hotel6"],7));
		}


		//获取房型信息
		$sql="select * from ".DQ."hotelroom where hotelroom2=".$id." order by hotelroom18 desc";
		$fangxinrs=$hmr->cache(true)->query($sql);
		if($fangxinrs) $this->assign("fangxinrs",$fangxinrs);

		//获取房型图片
		$picrs=$picmr->getpiclist($rs[0]["hotel0"]);
		if($picrs) $this->assign("picrs",$picrs);


		//获取周边客栈
		$zhoubianhotel=$this->getzhoubianhotel($rs[0]["hotel0"],$rs[0]["hotel5"]);
		$this->assign("zhoubianhotel",$zhoubianhotel);


		//获取评论列表
		$plmr=A("Inn/pinglun");
		$pinglunlist=$plmr->getpinglunlist($rs[0]["hotel0"]);
		$totalpf=$plmr->getonehotelpingf($rs[0]["hotel0"]);
		//获取这个客栈的每一项的评分
		$everypl=$plmr->getpinglunhotelev($rs[0]["hotel0"]);


		//获取猜你喜欢酒店客栈
		$cnxhh=$this->getxihuanhotellist($rs[0]["hotel5"], $rs[0]["hotel0"]);



		$this->assign("everypl",$everypl);
		$this->assign("totalpf",$totalpf);
		$this->assign("pinglunlist",$pinglunlist);
		$this->assign("cnxhh",$cnxhh);






		//获取回复列表
		$mel=A("Inn/messagesys");
		$messl=$mel->getmlistofhotel($rs[0]["hotel0"]);
		$this->assign("messl",$messl);


		//获取客栈类别信息
		$sql="select * from ".DQ."classsys where classsys0=".$rs[0]["hotel4"];
		$clsrs=M()->query($sql);
		$this->assign("clsrs",$clsrs[0]);




		$this->assign("rs",$rs[0]);
		$this->display();
	}


	/**
	 * 每执行一次就给一个客栈增加人气值1
	 */
	function addrenqi(){
		$hid=$_REQUEST["hid"];//酒店id值
		if(!is_numeric($hid)) die("dopass");
		$mr=M("hotel");
		if($mr->where('hotel0='.$hid)->setInc('hotel26',1)){
			echo "dook";
		} else{
			echo "dopass";
		}
	}

	/**
	 * 添加客栈特色内容
	 */
	function addteshe(){
		$hid=$_REQUEST["hid"];//酒店id值
		$teshecontent=$_REQUEST["teshecontent"];
		if(!is_numeric($hid)) die("dopass");


		$temp=getsubstr($teshecontent,"/^[\s,，.。'\"]+$/");
		if($temp!="" or $teshecontent=="") die("dopass");

		$sql="update ".DQ."hotel set hotel27=concat(hotel27,',".$teshecontent."') where hotel0=".$hid;

		$mr=M("hotel");
		$rs=$mr->execute($sql);
		if($rs){
			echo "dook";
		} else{
			echo "dopass";
		}
	}

	/**
	 * 通过cookie获取浏览过的客栈名称
	 */
	function getbrowerhotebycookie(){
		$arr=unserialize($_COOKIE["browerhotel"]);
		$this->assign("rs",$arr);
		$this->display();
	}

	/**
	 * 把浏览过的一个酒店地址添加到cookie值里
	 */
	function addbrowerhoteltocookie(){
		$hid=$_REQUEST["hid"];//酒店id
		$hname=$_REQUEST["hname"];//酒店名称
		if(!is_numeric($hid)) die("dopass");
		if($hname=="") die("dopass");
		$arr=unserialize($_COOKIE["browerhotel"]);
		$arr[$hid]=$hname;
		setcookie("browerhotel",serialize($arr),time()+13600,"/","");
	}

	/**
	 * 获取周边酒店列表,查找同类别下的其它客栈
	 */
	function getzhoubianhotel($hid,$cid){

		if(!is_numeric($hid) or !is_numeric($cid)) return null;

		$mr=M("hotel");
		$sql="select hotel0,hotel1 from ".DQ."hotel where hotel5=".$cid." and hotel0<>".$hid."
		    order by hotel25 desc,hotel28 desc limit 0,10";
		$rs=$mr->query($sql);
		return $rs?$rs:null;

	}

	/**
	 * 获取猜你喜欢的酒店列表,规则暂时是同市下面的随机几个客栈
	 * @param $cid
	 */
	function getxihuanhotellist($cid,$hid){
		if(!is_numeric($cid)) return "";
		if(!is_numeric($hid)) return "";
		$sql="select hotel0,hotel1,hotel15 from ".DQ."hotel where hotel5 in
		   (select classsys0 from ".DQ."classsys where classsys2=
		   	(select classsys2 from ".DQ."classsys where classsys0=$cid))
		   	and hotel0<>$hid  limit 0,10
		   	";
		$mr=M("hotel");
		$rs=$mr->query($sql);
		return ($rs)?$rs:"";
	}

	/**
	 * 酒店列表页
	 */
	function hotellist(){
        //城市列表
        $innAreaDao = D('InnArea');
        $innAreaProvince = S('innAreaProvince');
        if(empty($innAreaProvince)) {
            $innAreaProvince = $innAreaDao
                ->field($innAreaDao->_map_flip)
                ->where(array(
                    $innAreaDao->_map['menuId']=>590,
                    $innAreaDao->_map['parent']=>0
                ))
                ->order($innAreaDao->_map['order'])
                ->select();
            S('innAreaProvince', $innAreaProvince);
        }
        $innCity = S('innCity');
        if(empty($innCity)) {
            $innCity = array();
            $provinceCount = count($innAreaProvince);
            for($i=0; $i<$provinceCount; $i++) {
                $provinceId = $innAreaProvince[$i]['id'];
                $innCity[$provinceId] = $innAreaDao
                    ->field($innAreaDao->_map_flip)
                    ->where(array(
                        $innAreaDao->_map['menuId']=>590,
                        $innAreaDao->_map['parent']=>$provinceId
                    ))
                    ->order($innAreaDao->_map['order'])
                    ->select();
            }
            S('innCity', $innCity);
        }
        $this->assign('innAreaProvince', $innAreaProvince);
        $this->assign('innCity', $innCity);

        //客栈类型列表
		$innTypeDao = D('InnType');
        $innType = $innTypeDao
            ->field($innTypeDao->_map_flip)
            ->where(array(
                $innTypeDao->_map['menuId']=>606,
                $innTypeDao->_map['parent']=>0
            ))
            ->select();
        $innTypeList = makearray($innType, 'id', 'name');
        $this->assign('innType', $innType);
        $this->assign('innTypeList', $innTypeList);

        //客栈等级列表
		$innClassDao = D('InnClass');
        $innClass = $innClassDao
            ->field($innClassDao->_map_flip)
            ->where(array(
                $innClassDao->_map['menuId']=>598,
                $innClassDao->_map['parent']=>0
            ))
            ->select();
        $this->assign('innClass', $innClass);

        //客栈主题列表
		$innSubjectDao = D('InnSubject');
        $innSubject = $innSubjectDao
            ->field($innSubjectDao->_map_flip)
            ->where(array(
                $innSubjectDao->_map['menuId']=>613,
                $innSubjectDao->_map['parent']=>0
            ))
            ->select();
        $this->assign('innSubject', $innSubject);

        //推荐方式列表
		$innRecommendDao = D('InnRecommend');
        $innRecommend = $innRecommendDao
            ->field($innRecommendDao->_map_flip)
            ->where(array(
                $innRecommendDao->_map['menuId']=>614,
                $innRecommendDao->_map['parent']=>0
            ))
            ->select();
        $this->assign('innRecommend', $innRecommend);

        //预定方式列表
		$innBookTypeDao = D('InnBookType');
        $innBookType = $innBookTypeDao
            ->field($innBookTypeDao->_map_flip)
            ->where(array(
                $innBookTypeDao->_map['menuId']=>615,
                $innBookTypeDao->_map['parent']=>0
            ))
            ->select();
        $this->assign('innBookType', $innBookType);
        /***********************************************************************
         * 解析筛选条件，构造筛选URL
         **********************************************************************/
        $innDao = D('Inn');
        $title = '';//网页标题
        $keyword = '';//网页关键字
        $description = '';//网页介绍
        $baseUrl = __ACTION__;//基础URL
        $condition = array();//查询条件
        $condition[$innDao->_map['menuId']] = 585;//客栈数据标志
        $condition[$innDao->_map['status']] = 1212;//审核标志
        //查询城市
        $city = $_GET['c'];
        if(!empty($city)) {
            $baseUrl .= '-c-'.$city;
            $condition[$innDao->_map['area']] = $city;
        }

        //查询关键字
        $keyword = $_GET['k'];
        if(!empty($keyword)) {
            $baseUrl .= '-k-'.$keyword;
            $condition[$innDao->_map['name']] = array('like', '%'.$keyword.'%');
        }
        $this->assign('baseUrl', $baseUrl);

        //分析快捷筛选条件
        $baseParam = array();
        if(!empty($_GET['s'])) {
            $baseParam = preg_split('/_/',$_GET['s']);
        }
        $this->assign('baseParam', $baseParam);

        //分析客栈类型
        if(!empty($baseParam[0])) {
            $condition[$innDao->_map['type']] = $baseParam[0];
            foreach($innType as $row) {
                if($row['id'] == $baseParam[0]) {
                    $title = $row['name'] . $title;
                    break;
                }
            }
        }
        //分析客栈级别
        if(!empty($baseParam[1])) {
            $condition[$innDao->_map['class']] = $baseParam[1];
            foreach($innClass as $row) {
                if($row['id'] == $baseParam[1]) {
                    $title = $row['name'] . $title;
                    break;
                }
            }
        }
        //分析客栈主题
        if(!empty($baseParam[2])) {
            $condition[$innDao->_map['subject']] = $baseParam[2];
            foreach($innSubject as $row) {
                if($row['id'] == $baseParam[2]) {
                    $title = $row['name'] . $title;
                    break;
                }
            }
        }
        //分析客栈推荐方式
        if(!empty($baseParam[3])) {
            $condition[$innDao->_map['recommend']] = $baseParam[3];
            foreach($innRecommend as $row) {
                if($row['id'] == $baseParam[3]) {
                    $title = $row['name'] . $title;
                    break;
                }
            }
        }
        //分析客栈预定方式
        if(!empty($baseParam[4])) {
            $condition[$innDao->_map['booktype']] = $baseParam[4];
            foreach($innBookType as $row) {
                if($row['id'] == $baseParam[4]) {
                    $title .= '-支持' . $row['name'];
                    break;
                }
            }
        }
        if(!empty($title)) $this->assign('detailTitle', $title);

        //$condition[$innDao->_map['area']] = 585;

        //分页
        import("ORG.Util.Page");
        $innCount = $innDao->where($condition)->count();
        $Page = new Page($innCount, 6);
        $Page->setConfig('header','间客栈');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','首页');
        $Page->setConfig('last','末页');
        $Page->setConfig('theme','<li><a>(%totalRow%)</a>%linkPage%</li> <li class="f1">%first% %upPage% %downPage% %end%</li>');
        $this->assign('pageBar', $Page->show());

        //取数据
        $innList = $innDao
            ->field($innDao->_map_flip)
            ->where($condition)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        //分析评分和最低价
        $InnRoom = D('InnRoom');
		$plmr=A("Inn/pinglun");
        for($i = 0; $i < $innCount; $i++) {
            //评分
            $innList[$i]['score'] = $plmr->getonehotelpingf($innList[$i]['id']);
            //最低价
            $minprice = $InnRoom
                ->field("min(".$InnRoom->_map['minprice'].") as minprice")
                ->where($InnRoom->_map['innId'] . '=' . $innList[$i]['id'])
                ->find();
            $innList[$i]['minprice'] = $minprice['minprice'];
        }
        $this->assign('innList', $innList);
        /***********************************************************************
         * 取推荐客栈2条
         **********************************************************************/
        $recommendCondition[$innDao->_map['status']] = 1212;
        if(!empty($condition[$innDao->_map['area']]))
            $recommendCondition[$innDao->_map['area']] = $condition[$innDao->_map['area']];
        //取数据
        $recommendInnList = $innDao
            ->field($innDao->_map_flip)
            ->where($recommendCondition)
            ->order('RAND()')//TODO 推荐度排序字段
            ->limit(2)
            ->select();
        //分析评分和最低价
		$plmr=A("Inn/pinglun");
        for($i = 0; $i < count($recommendInnList); $i++) {
            $recommendInnList[$i]['score'] = $plmr->getonehotelpingf($recommendInnList[$i]['id']);
            $minprice = $InnRoom
                ->field("min(".$InnRoom->_map['minprice'].") as minprice")
                ->where($InnRoom->_map['innId'] . '=' . $recommendInnList[$i]['id'])
                ->find();
            $recommendInnList[$i]['minprice'] = $minprice['minprice'];
        }
        $this->assign('recommendInnList', $recommendInnList);
        /***********************************************************************
         * 取相关线路
         **********************************************************************/
        //分析城市名
        $cityName = '';
        if(!empty($city)) {
            $cityData = $innAreaDao
                ->field($innAreaDao->_map_flip)
                ->where(array(
                    $innAreaDao->_map['menuId']=>590,
                    $innAreaDao->_map['id']=>$city
                ))
                ->find();
            $cityName = $cityData['name'];
            $this->assign('cityName', $cityName);
        }

        $hoteltour = S('hoteltour' . $city);
        if(empty($hoteltour)) {

            global $SYS_config;
            $lead = urlencode( iconv("UTF-8", "gb2312", $cityName) );
            $url = $SYS_config["Gettourxoldurl"]."/xdreams.asp"
                 . "?lead={$lead}&tourtype=zhoubian&format=hotelListSide&limit=9";

            $hoteltour = file_get_contents($url);
            $hoteltour = iconv('GB2312', 'UTF-8', $hoteltour);

            S('hoteltour' . $city , $hoteltour, 3600);
        }
        if(!empty($hoteltour)) $this->assign('hoteltour', $hoteltour);

        /***********************************************************************
         * 取相关景点游记、图片
         **********************************************************************/
        //远程获取数据
        $parseArray = C('TMPL_PARSE_STRING');
        $url  = $parseArray['__UCHOME__'];
        $url .= '/user.php?m=Json&a=BlogList&limit=15&keyword=中国'.$cityName;
        $data = uc_fopen($url);

        if(!empty($data) && ($bloglist = json_decode($data)) && ($bloglist=object2array($bloglist))) {
            $topBlogList = array_slice($bloglist, 0, 2);//游记头两条
            $otherBlogList = array_slice($bloglist, 2, 7);//其他游记
            $picBlogList = array_slice($bloglist, 9, 6);//相关景点图片
            if(!empty($topBlogList)) $this->assign('topBlogList', $topBlogList);
            if(!empty($otherBlogList)) $this->assign('otherBlogList', $otherBlogList);
            if(!empty($picBlogList)) $this->assign('picBlogList', $picBlogList);
        }
        /***********************************************************************
         * 取相相关统计数据
         **********************************************************************/
        if(!($innStat = S('innStat'))) {
            $innStat["checkin"] = rand(100,999);
            $innStat["inncount"] = rand(100,999);
            $innStat["vister"] = rand(100,999);
            $innStat["subject"] = rand(100,999);
            $innStat["order"] = rand(100,999);
            $innStat["time"] = time();
            S('innStat', $innStat);
        }
        //更新统计
        if($innStat["time"] < time() - 5) {
            $innStat["checkin"] += rand(0,3);
            //$innStat["inncount"] = rand(100,999);
            $innStat["vister"] += rand(-50,50);
            $innStat["subject"] += rand(0,3);
            $innStat["order"] += rand(0,3);
            $innStat["time"] = time();
            S('innStat', $innStat);
        }
        $this->assign('innStat', $innStat);

		$this->display();
	}
}

?>